define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'borrowing/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'borrowing/multi',
                    table: 'borrow',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'borrowId',
                sortName: 'borrowId',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'borrowId', title: __('BorrowId'),operate:false},
                        {field: 'user.name', title: __('Phone'),operate:false},
                        {field: 'name', title: __('Name')},
                        //{field: 'updateTime', title: __('UpdateTime'), formatter: Table.api.formatter.datetime},
                        {field: 'type1', title: __('Type1'),operate:false},
                        //{field: 'pledge', title: __('Pledge')},
                        {field: 'borrowtime', title: __('Borrowtime'),operate:false},
                        //{field: 'time', title: __('Time'), formatter: Table.api.formatter.datetime},
                        //{field: 'repayType', title: __('RepayType')},
                        //{field: 'useCards', title: __('UseCards')},
                        {field: 'account', title: __('Account'),operate:false},
                        {field: 'raiseAccount', title: __('RaiseAccount'),operate:false},
                        {field: 'tendercount', title: __('Tendercount'),operate:false},

                        //{field: 'aprBase', title: __('AprBase')},
                        //{field: 'aprAdd', title: __('AprAdd')},
                        {field: 'aprTotal', title: __('AprTotal'),operate:false},
                        //{field: 'limitMoney', title: __('LimitMoney')},
                        //{field: 'validDay', title: __('ValidDay')},
                        //{field: 'interest', title: __('Interest')},
                        //{field: 'totalAccount', title: __('TotalAccount')},
                        //{field: 'repayAccount', title: __('RepayAccount')},
                        //{field: 'startTime', title: __('StartTime'), formatter: Table.api.formatter.datetime},
                        //{field: 'endTime', title: __('EndTime'), formatter: Table.api.formatter.datetime},
                        //{field: 'createTime', title: __('CreateTime'), formatter: Table.api.formatter.datetime},
                        {field: 'status1', title: __('Status1'),operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: function (value, row, index) {
                            var detail = '<a class="btn btn-xs btn-success btn-detail">查看</a> ';
                            return detail + Table.api.formatter.operate.call(this, value, row, index, table);
                        }}
                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                operate: function (value, row, index) {
                    //返回字符串加上Table.api.formatter.operate的结果
                    //默认需要按需显示排序/编辑/删除按钮,则需要在Table.api.formatter.operate将table传入
                    //传入了table以后如果edit_url为空则不显示编辑按钮,如果del_url为空则不显显删除按钮
                    return '<a class="btn btn-info btn-xs btn-detail"><i class="fa fa-list"></i> ' + __('Detail') + '</a> '
                        + Table.api.formatter.operate(value, row, index, $("#table"));
                },
            },
            events: {//绑定事件的方法
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('borrows/detail/ids/' + row['borrowId'], __('Detail'));
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});